package com.brand.prisd.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.brand.prisd.R;
import com.brand.prisd.bean.UserBean;
import com.tuya.smart.android.user.api.IRegisterCallback;
import com.tuya.smart.android.user.api.IValidateCallback;
import com.tuya.smart.android.user.bean.User;
import com.tuya.smart.home.sdk.TuyaHomeSdk;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 注册界面
 */
public class RegisterActivity extends AppCompatActivity {

    @BindView(R.id.iv_left_arrow)
    ImageView ivLeftArrow;
    @BindView(R.id.et_phoneNum)
    EditText etPhoneNum;
    @BindView(R.id.btn_getSecurityCode)
    Button btnGetSecurityCode;
    @BindView(R.id.et_SecurityCode)
    EditText etSecurityCode;
    @BindView(R.id.et_psw)
    EditText etPsw;
    @BindView(R.id.et_confirm_psw)
    EditText etConfirmPsw;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @BindView(R.id.tv_hint)
    TextView tvHint;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        etPsw.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setHintTextVisibility();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void setHintTextVisibility() {
        boolean focusable = etPsw.isFocusable();
        if (focusable) {
            tvHint.setVisibility(View.VISIBLE);
        } else {
            tvHint.setVisibility(View.GONE);
        }
    }

    private void getSecurityCode() {
        String phoneNum = etPhoneNum.getText().toString();
        if (TextUtils.isEmpty(phoneNum)) {
            Toast.makeText(this, "请检查是否输入正确！", Toast.LENGTH_SHORT).show();
            return;
        }
        //获取手机验证码
        TuyaHomeSdk.getUserInstance().getValidateCode("86", phoneNum, new IValidateCallback() {
            @Override
            public void onSuccess() {
                Toast.makeText(getApplicationContext(), "获取验证码成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(String code, String error) {
                Toast.makeText(getApplicationContext(), "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void SignUpForAPasswordAccount(UserBean user, String code) {
        //注册手机密码账户
        TuyaHomeSdk.getUserInstance().registerAccountWithPhone(user.getCountryCode(), user.getPhone(), user.getPsw(), code, new IRegisterCallback() {
            @Override
            public void onSuccess(User user) {
                String phoneCode = user.getPhoneCode();

                Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(String code, String error) {
                Toast.makeText(getApplicationContext(), "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @OnClick({R.id.iv_left_arrow, R.id.btn_getSecurityCode, R.id.btn_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_left_arrow:
                go2SelectAccountActivity();
                break;
            case R.id.btn_getSecurityCode:
                getSecurityCode();
                break;
            case R.id.btn_confirm:

                break;
        }
    }

    private void go2SelectAccountActivity() {
        startActivity(new Intent(this, SelectAccountActivity.class));
        finish();
    }

    @Override
    public void onBackPressed() {
        go2SelectAccountActivity();
    }
}
